package com.linxz.video.appui.publish;
import com.linxz.framework.base.ActivityLifeCycleEvent;
import com.linxz.framework.base.module.BaseModel;
import com.linxz.framework.protocol.callback.HttpCallBack;
import com.linxz.framework.protocol.callback.ModelCallBack;
import com.linxz.video.pojo.FileUploadResp;
import com.linxz.video.protocol.VideoApiStore;

import java.io.File;

import io.reactivex.subjects.PublishSubject;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;
/**
 * @author Linxz
 * 创建日期：2020年06月19日 14:03
 * version：v4.5.4
 * 描述：
 */
public class FileUpLoadModule extends BaseModel {

    private VideoApiStore mVideoApiStore;

    public FileUpLoadModule(PublishSubject<ActivityLifeCycleEvent> lifecycleSubject) {
        super(lifecycleSubject);
        mVideoApiStore=getOutApiStore(VideoApiStore.class);
    }

    private void uploadFile(String filePath, final ModelCallBack<String> modelCallBack) {
        File file = new File(filePath);
        RequestBody fileRQ = RequestBody.create(MediaType.parse("multipart/form-data"), file);
        MultipartBody.Part fileBody=MultipartBody.Part.createFormData("file",file.getName(),fileRQ);
        toSubscribe(mVideoApiStore.fileUpload(RequestBody.create(MultipartBody.FORM,"dynamic"),fileBody), new HttpCallBack<FileUploadResp>() {
            @Override
            public void onErrors(String code, String msg) {
                modelCallBack.onErrors(code, msg);
            }

            @Override
            public void onResponse(FileUploadResp fileUploadResp) {
                if (fileUploadResp.isOk()) {
                    modelCallBack.onSuccess(fileUploadResp.getData());
                  } else {
                    modelCallBack.onErrors(fileUploadResp.getStatus(), fileUploadResp.getMsg());
                }
            }
        });
    }

    private void pushVideoDynamic(String tittle,
                                  String description,
                                  String videoSeconds,
                                  String videoWidth,
                                  String videoHeight,
                                  String coverUrl,
                                  String bmgId,
                                  String label,
                                  String videoPath,
                                  final ModelCallBack<String> modelCallBack) {
        File file = new File(videoPath);
        RequestBody fileRQ = RequestBody.create(MediaType.parse("multipart/form-data"), file);
        RequestBody body = new MultipartBody.Builder()
                .addFormDataPart("tittle", tittle)
                .addFormDataPart("description", description)
                .addFormDataPart("videoSeconds", videoSeconds)
                .addFormDataPart("videoWidth",videoWidth)
                .addFormDataPart("videoHeight", videoHeight)
                .addFormDataPart("coverUrl", coverUrl)
                .addFormDataPart("bmgId", bmgId)
                .addFormDataPart("label", label)
                .addFormDataPart("file", file.getName(), fileRQ)
                .build();

        MultipartBody.Part fileBody=MultipartBody.Part.createFormData("file",file.getName(),fileRQ);
        toSubscribe(mVideoApiStore.uploadVideo(
                fileBody,
                RequestBody.create(MultipartBody.FORM,bmgId),
                RequestBody.create(MultipartBody.FORM,tittle),
                RequestBody.create(MultipartBody.FORM,description),
                RequestBody.create(MultipartBody.FORM,videoSeconds),
                RequestBody.create(MultipartBody.FORM,videoHeight),
                RequestBody.create(MultipartBody.FORM,videoWidth),
                RequestBody.create(MultipartBody.FORM,coverUrl)), new HttpCallBack<FileUploadResp>() {
            @Override
            public void onErrors(String code, String msg) {
                modelCallBack.onErrors(code, msg);
            }

            @Override
            public void onResponse(FileUploadResp fileUploadResp) {
                if (fileUploadResp.isOk()) {
                    modelCallBack.onSuccess(fileUploadResp.getData());
                } else {
                    modelCallBack.onErrors(fileUploadResp.getStatus(), fileUploadResp.getMsg());
                }
            }
        });
    }


    public void push(String tittle,
                      String description,
                      String videoSeconds,
                      String videoWidth,
                      String videoHeight,
                      String coverFilePath,
                      String bmgId,
                      String label,
                      String videoPath,
                      final ModelCallBack<String> modelCallBack) {
        uploadFile(coverFilePath, new ModelCallBack<String>() {
            @Override
            public void onSuccess(String coverUrl) {
                pushVideoDynamic(tittle,
                        description,
                        videoSeconds,
                        videoWidth,
                        videoHeight,
                        coverUrl,
                        bmgId,
                        label,
                        videoPath,
                        new ModelCallBack<String>() {
                            @Override
                            public void onSuccess(String model) {
                                modelCallBack.onSuccess(model);
                            }

                            @Override
                            public void onErrors(String code, String msg) {
                                modelCallBack.onErrors(code,msg);
                            }
                        });
            }

            @Override
            public void onErrors(String code, String msg) {
                modelCallBack.onErrors(code,"封面上传失败");
            }
        });
    }

}
